package jiqiao;

/**
 * @author understand
 * @description:
 * @create 2025/5/18 17:05
 */
public class FindDuplicate {
    public static void main(String[] args) {
        int[] nums = {4,3,1,4,2};
        System.out.println(findDuplicate(nums));
    }
    public static int findDuplicate(int[] nums) {
        int slow = 0,fast = 0;
        int n = nums.length;
        while (true){
            slow = nums[slow];
            fast = nums[nums[fast]];
            if (slow == fast)
                break;
        }
        slow = 0;
        while (slow!=fast){
            slow = nums[slow];
            fast = nums[fast];
        }
        return slow;
    }
}
